$(function () {
    /*initSNU({
        id:'helpContext',
        type:1,
        toolsArr:null,
        height:360,
        gs:true,
        video:true
    });*/

    initSummerNote($("#helpContext"));

    $("body").on('change', '#showClients', function() {
        var _value = $(this).val();
        if (_value == 1) {
            $('.frontBottom').hide();
        } else {
            $('.frontBottom').show();
        }
    });



    /**
     * 初始化富文本编辑器
     * @param target 选择器
     */
    function initSummerNote(target) {
        var toolsArr = [
            // 撤消, 重做
            ["remove", ["undo", "redo"]],
            // 字体,字体大小,字体颜色,粗体,斜体
            ["font", ["fontname", "fontsize", "color", "bold", "italic"]],
            // 样式,序列表,无序列表,段落,行高
            ["para", ["style", "ol", "ul", "paragraph", "height"]],
            // 链接插入 ,图片上传 ,视频 ,表格, <hr/>
            ["insert", ["link", "myImage", "myvideo", "table", "hr"]],
            //下划线,中划线,居上,居下,清除格式
            ["para", ["underline", "strikethrough", "superscript", "subscript", "clear"]],
            //源码HTML转换，全屏，公式
            ["view", ["codeview", "fullscreen", "myGS"]]
        ];

        target.summernote({
            height: 520,
            lang: 'zh-CN',
            toolbar: toolsArr,
            buttons: {
                myvideo: function (context) {
                    var ui = $.summernote.ui;
                    // create button
                    var button = ui.button({
                        contents: '<i class="note-icon-video"/>',
                        tooltip: '视频',
                        click: function () {
                            var winContext = '<div class="m-t m-r m-l m-b">';
                            winContext += '<select id="videoType" class="form-control mb10">';
                            winContext += '<option value="96K">268云服务</option>';
                            winContext += '</select>';
                            winContext += '<input class="form-control" value="" id="vidoCode" type="text">';
                            winContext += '</div>';

                            var index = parent.layer.open({
                                type: 1,
                                shadeClose: false,
                                area: ['380px'],
                                skin: 'layui-layer-demo',
                                title: '请填写视频播放码',
                                content: winContext,
                                btn: '确定',
                                yes: function (index, layero) {
                                    var vidoCode = layero.find("#vidoCode").val();
                                    var videoType = layero.find("#videoType").val();
                                    if (videoType == '96K') {
                                        vidoCode = $.trim(vidoCode);
                                        var div = document.createElement('div');
                                        div.innerHTML = '<object width="99%" height="99%" id="96k_'+vidoCode+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param value="http://v.96koo.net/static/video/96k.swf?videoId='+vidoCode+'" name="movie"><param name="wmode" value="transparent"><param value="true" name="allowFullScreen"><param value="always" name="allowScriptAccess"><param value="videoId='+vidoCode+'" name="FlashVars"><embed width="99%" height="99%" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" name="96k_'+vidoCode+'" src="https://v.96koo.net/static/video/96k.swf?videoId='+vidoCode+'"></object>';
                                        context.invoke('editor.insertNode', div);
                                    }
                                    parent.layer.close(index);
                                }
                            });
                        }
                    });
                    return button.render();
                },
                myGS: function () { // 此处是公式的图
                    var ui = $.summernote.ui;
                    var button = ui.button({
                        contents: '<i class="fa fa-gbp fs16"/>',
                        tooltip: '选择KityFormula公式',
                        click: function () {
                            // parent.layer.closeAll();
                            var winContext = baseloaction + '/kityformula-plugins/kityFormulaLayer.html#' + mydomain;
                            parent.layer.open({
                                type: 2,
                                shadeClose: false,
                                area: ['820px', '545px'],
                                skin: 'layui-layer-demo',
                                title: '插入KityFormula公式',
                                content: winContext,
                                btn: '确定',
                                yes: function (index) {
                                    window.parent.frames['layui-layer-iframe' + index].getImageURL(function (imgUrl) {
                                        target.summernote('insertImage', imgUrl);
                                        parent.layer.close(index);
                                    });
                                }
                            });
                        }
                    });
                    return button.render();
                },
                myImage: function () { // 此处是上传的图
                    var ui = $.summernote.ui;
                    var button = ui.button({
                        contents: '<i class="note-icon-picture"/>',
                        tooltip: '上传图片',
                        click: function () {
                            var winContext = '<div class="m-t m-r m-l m-b">';
                            winContext += '选择图片<input type="file" id="fileInput" class="form-control"/><br/>';
                            winContext += '或填写图片路径<input class="form-control" value="" id="imageUrl" type="text"/>';
                            winContext += '</div>';
                            var index = parent.layer.open({
                                type: 1,
                                shadeClose: false,
                                area: ['500px'],
                                skin: 'layui-layer-demo',
                                title: '插入图片',
                                content: winContext,
                                btn: '确定',
                                yes: function (index, layero) {
                                    var file = layero.find("#fileInput")[0].files[0];
                                    var imageUrl = layero.find("#imageUrl").val();
                                    if (typeof file == 'undefined' && (imageUrl == null || $.trim(imageUrl) == '')) {
                                        icModale(0, "请选择图片或填写有效的图片路径");
                                        return;
                                    }
                                    if (imageUrl == null || $.trim(imageUrl) == '') {
                                        var reader = new FileReader();
                                        reader.onload = (function (f) {
                                            return function (e) {
                                                imageUrl = this.result;
                                                target.summernote('insertImage', imageUrl, function ($image) {
                                                    $image.css({
                                                        'max-width': '100%',
                                                        'height' : 'auto'
                                                    });
                                                    parent.layer.close(index);
                                                });
                                            };
                                        })(file);
                                        reader.readAsDataURL(file);
                                    }else {
                                        target.summernote('insertImage', imageUrl, function ($image) {
                                            $image.css({
                                                'max-width': '100%',
                                                'height' : 'auto'
                                            });
                                            parent.layer.close(index);
                                        });
                                    }
                                }
                            });
                        }
                    });
                    return button.render();
                }
            },
            callbacks: {
                onImageUpload: function (files) {
                    var file = files[0];
                    var fileData = new FormData();
                    fileData.append("imgFile", file);
                    $.ajax({
                        url: uploadPath + '/image/upload/editBox',
                        type: 'post',
                        data: fileData,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (result) {
                            var obj = JSON.parse(result);
                            if (obj.success == true) {
                                target.summernote('insertImage', obj.entity, function ($image) {
                                    // max-width max-height 限制图的最大高宽 e-tg-txt-pic 图点击放大 及 试题列表页不显示图
                                    $image.css('max-width', 300).css('max-height', 300).addClass('e-tg-txt-pic');
                                });
                            } else {
                                icModale(0, obj.message);
                            }
                        },
                        error: function (error) {
                            icModale(0, "系统错误，文件上传失败");
                        }
                    });
                }
            }
        });
    }
});